package wjh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import wjh.pojo.*;
import wjh.service.AdminService;
import wjh.service.PlanService;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/8/20 18:02
 * 控制层 控制器
 */
@Controller
//1.加载到spring容器
//2.控制器需要读
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    //可能出现登录失败 使用model保存失败信息
    //session保存成功信息
    // 用到参数在控制器方法参数中写上
    public String login(Admin admin, Model model, HttpSession session) {

        Admin admin1 = adminService.login(admin);
        if (admin1 == null) {
            model.addAttribute("msg", "用户名密码错误！");
            //登录失败 返回到login.jsp
            return "login";
        } else {
            session.setAttribute("admin", admin1);
            //登录成功，跳转到index.jsp
            return "index";
        }
    }

    //患者所在科室及人数
    @RequestMapping("/echarts")
    public String echarts(Model model) {
        //患者所在科室及人数
        List<Num> numList = adminService.echarts();
        //患者男女比例
        List<Sex> paList = adminService.echarts2();
        model.addAttribute("numList", JSONArray.toJSONString(numList));
        model.addAttribute("paList", JSONArray.toJSONString(paList));
//        Map<String, Integer> map = adminService.findAgeGroupCounts();
        Map<String, Integer> map = adminService.findAgeGroupCounts();
        //HashMap转Json
        //JSONObject构建Json对象
        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            //使用put添加键值对
            jsonObject.put(entry.getKey(),entry.getValue());
        }
        //将jsonObject转换成字符串形式的JSON
        String json1 = jsonObject.toString();
        model.addAttribute("json1",json1);
//        List<Integer> ageList = adminService.findAllAges();
//        model.addAttribute("ageList",ageList);
        return "echarts";
    }


    //新建排班
    @RequestMapping("/addPaiBan")
    public String addPaiBan(Plan plan, String doctor, Integer num1, String week, Model model) {
//        System.out.println(doctor+"---"+num1+"---"+week);
        String[] s1 = doctor.split("-");
        int doctor_id = Integer.parseInt(s1[0]);
        Plan plan1 = planService.findByIdAndWeek(doctor_id, week);
        //排班校验 同一个医生一天只能有一条排班记录
        if (plan1 != null) {
            model.addAttribute("msg", "该医生该日已排班");
            return "paiBan";
        } else {
            plan.setDoctor_id(doctor_id);
            plan.setNumber(num1);
            plan.setWeek(week);
            adminService.addPaiBan(plan);
//       return "paiBan";
            return "redirect:/plan/findAllPai.do";

        }
    }

    @Autowired
    private PlanService planService;

    @RequestMapping("/findAllNum")
    @ResponseBody
    public List<PlanVo> findAllNum() {
        List<PlanVo> planList = planService.findAllPai();
        return planList;
    }

    //根据医生id和周几查找排班记录
    @RequestMapping("/findByIdAndWeek")
    @ResponseBody
    public Plan findByIdAndWeek(Integer id, String week) {
        Plan plan = planService.findByIdAndWeek(id, week);
        return plan;
    }

    //根据plan.ids删除排班信息
    @RequestMapping("/delPaisById")
    public String delPaisById(String ids) {
        String[] split = ids.split("-");
        for (String id : split) {
            planService.delPaisById(Integer.parseInt(id));
        }
        return "redirect:/plan/findAllPai.do";
    }

    @RequestMapping("/toUpdatePai")
    //接收参数 调用方法 做出响应
    public String toUpdateUser(Integer pageNum,Integer pageSize,Integer id,Model model) {

        Plan plan = planService.findPaiById(id);
        model.addAttribute("plan",plan);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        //跳转到修改界面
        return "updatePaiBan";
    }

    @RequestMapping("/updatePaiBan")
    //接收参数 调用方法 做出响应
    public String updatePaiBan(Integer pageNum, Integer pageSize, Integer id, Model model,
                               String doctor, Integer num1, String week, RedirectAttributes attr) {
        String[] s1 = doctor.split("-");
        int doctor_id = Integer.parseInt(s1[0]);
        Plan plan1 = planService.findByIdAndWeek(doctor_id, week);
        //排班校验 同一个医生一天只能有一条排班记录
        if (plan1 != null) {
            model.addAttribute("msg", "该医生该日已排班");
            return "updatePaiBan";
        } else {
            Plan plan = new Plan(id,doctor_id,num1,week);
//            System.out.println(plan.getId()+"-"+plan.getDoctor_id()+"-"+plan.getNumber()+"-"+plan.getWeek());
            adminService.updatePaiBan(plan);
            attr.addAttribute("pageNum",pageNum);
            attr.addAttribute("pageSize",pageSize);
            return "redirect:/plan/findAllPai.do";
        }
    }

}